<?php

namespace App\Services\ApplyGoldCoinWithdrawXzbPay;

use Exception;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Withdraw;
use App\Models\Freelance;
use App\Jobs\UserWithdrawJob;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\ValidationException;
use App\Supports\WithdrawFreelance\XinZhongBao\XinZhongBao;

class AuthGoldCoinWithdrawXzbPay 
{
	
	public function __construct(User $user)
	{
		$this->user = $user;
	}


	public function auth()
	{
		try {
			if ($this->user->freelance) {
				abort(400, '用户已认证');
			}

			/*if (Freelance::where('id_no',request('id_no'))->first()) {
				abort(400, '身份证已被使用');
			}*/
			$freelance = app(XinZhongBao::class, ['user' => $this->user]);
			$params = [
				'name' => request('real_name'),
				'idCardNo' => request('id_no'),
			];

			$res = $freelance->addMerchant($params);

			if($res['code'] == '0000'){

				$freelance = Freelance::create([
					'real_name' => request('real_name'),
					'id_no' => request('id_no'),
					'id_card_front' => request('id_card_front'),
					'id_card_back' => request('id_card_back'),
					'is_auth' => true,
					'user_id' => $this->user->id,
				]);
			}else{

				abort(400, '认证失败');
			}

			return $freelance;

			
		} catch (Exception $exception) {
			throw ValidationException::withMessages(['err' => $exception->getMessage()]);
		}
	}

}
